package cn.edu.sgu.www.mhxysy.service.wenshi.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.wenshi.WenshiAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.wenshi.Wenshi;
import cn.edu.sgu.www.mhxysy.entity.wenshi.WenshiAttribute;
import cn.edu.sgu.www.mhxysy.entity.wenshi.WenshiCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.wenshi.WenshiAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.wenshi.WenshiCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.wenshi.WenshiMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.wenshi.WenshiAttributeService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.wenshi.WenshiAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class WenshiAttributeServiceImpl implements WenshiAttributeService {

    private final WenshiMapper wenshiMapper;
    private final WenshiCategoryMapper wenshiCategoryMapper;
    private final WenshiAttributeMapper wenshiAttributeMapper;

    @Autowired
    public WenshiAttributeServiceImpl(
            WenshiMapper wenshiMapper,
            WenshiCategoryMapper wenshiCategoryMapper,
            WenshiAttributeMapper wenshiAttributeMapper) {
        this.wenshiMapper = wenshiMapper;
        this.wenshiCategoryMapper = wenshiCategoryMapper;
        this.wenshiAttributeMapper = wenshiAttributeMapper;
    }

    @Override
    public void insert(WenshiAttributeDTO wenshiAttributeDTO) {
        WenshiAttribute wenshiAttribute = toEntity(wenshiAttributeDTO);

        // 查询纹饰信息
        String wenshiId = wenshiAttribute.getWenshiId();
        Wenshi wenshi = wenshiMapper.selectById(wenshiId);

        List<WenshiAttribute> list = wenshiAttributeMapper.selectByWenshiId(wenshiId);
        WenshiCategory wenshiCategory = wenshiCategoryMapper.selectById(wenshi.getCategoryId());

        /*
         * 1级纹饰有3条随机属性；2级属性有4条随机属性；3级属性有5条随机属性
         * 因此，纹饰的属性条数为：纹饰等级 + 2
         */
        int total = wenshiCategory.getGrade() + 2;

        // 验证纹饰属性条数
        if (list.size() >= total) {
            throw new GlobalException(ResponseCode.CONFLICT, "当前纹饰属性条数已达上限！");
        }

        wenshiAttribute.setId(wenshiAttribute.uuid());

        wenshiAttributeMapper.insert(wenshiAttribute);
    }

    @Override
    public void deleteById(String id) {
        wenshiAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(WenshiAttributeDTO wenshiAttributeDTO) {
        ObjectUtils.requireNonNull(wenshiAttributeDTO.getId(), CustomMessages.ID_NOT_NULL);

        WenshiAttribute wenshiAttribute = toEntity(wenshiAttributeDTO);

        wenshiAttributeMapper.updateById(wenshiAttribute);
    }

    @Override
    public List<String> selectByZuoqiId(String zuoqiId) {
        List<WenshiAttributeVO> list = wenshiAttributeMapper.selectByZuoqiId(zuoqiId);

        Stream<WenshiAttributeVO> stream = list.stream();

        return stream.map(new Function<WenshiAttributeVO, String>() {
            @Override
            public String apply(WenshiAttributeVO wenshiAttributeVO) {
                StringBuilder sb = new StringBuilder();

                sb.append(wenshiAttributeVO.getName());
                sb.append("+");

                /*
                 * 以百分号为单位的属性，在后面拼上%，并且总属性是小数表示的
                 * 如：物理防御+24、法术抗暴+1.0%
                 */
                BigDecimal total = wenshiAttributeVO.getTotal();

                if (wenshiAttributeVO.getIsPercent()) {
                    sb.append(total.doubleValue());
                    sb.append("%");
                } else {
                    sb.append(total.intValue());
                }

                return sb.toString();
            }
        }).collect(Collectors.toList());
    }

    @Override
    public List<String> selectAttributes(String wenshiId) {
        return wenshiAttributeMapper.selectAttributes(wenshiId);
    }

    @Override
    public List<WenshiAttribute> selectByWenshiId(String wenshiId) {
        if (StringUtils.isEmpty(wenshiId)) {
            return new ArrayList<>();
        }

        return wenshiAttributeMapper.selectByWenshiId(wenshiId);
    }

    private WenshiAttribute toEntity(WenshiAttributeDTO wenshiAttributeDTO) {
        WenshiAttribute wenshiAttribute = new WenshiAttribute();

        wenshiAttribute.setId(wenshiAttributeDTO.getId());
        wenshiAttribute.setWenshiId(wenshiAttributeDTO.getWenshiId());
        wenshiAttribute.setAttributeId(wenshiAttributeDTO.getAttributeId());

        return wenshiAttribute;
    }

}